<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="num1 == index ? 'active' : ''"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Tab(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isShow }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div class="loading" v-show="!isReachBottom">
        <i class="iconfont icon-loading"></i>
        加载中
      </div>
      <div class="loading" v-show="isReachBottom">加载完毕</div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { goodsSearchAPI } from "@/request/Api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";

export default {
  data() {
    return {
      // 栏目 目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      // ( 1->实体商品 2->虚拟商品 0->全部 )
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      // 少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 商品关键词
      keyword: "",
      // 商品列表
      goodsList: [],
      // 排序
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // options1高亮
      num1: 0,
      options2: [
        { txt: "全部", type: 0, isShow: true },
        { txt: "实物礼品", type: 1, isShow: false },
        { txt: "虚拟礼品", type: 2, isShow: false },
      ],
      goodsListShow: [],
      page: 1,
      size: 8,
      isReachBottom: false,
      isLoading: false,
    };
  },
  created() {
    this.keyword = this.$route.query.keyword;

    this.getAllGoods();
    // this.keyword = "";
    // this.$router.push("/goods");
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  components: {
    Crumb,
    List,
  },
  methods: {
    // 封装搜索请求
    async getAllGoods() {
      let res = await goodsSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.goodsList = res.data.data;
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);
      // console.log("全部商品", res);
    },
    options1Tab(idx, min, max) {
      this.num1 = idx;
      this.min = min;
      this.max = max;
      // console.log(this.min, this.max);
      this.getAllGoods();
    },
    options2Fn(idx, type) {
      // 排他控制高亮
      this.options2.forEach((item) => (item.isShow = false));
      this.options2[idx].isShow = true;
      this.type = type;
      this.getAllGoods();
    },
    // 滚动事件
    scrollFn() {
      if (getScrollTop() + getClientHeight() > getScrollHeight() - 300) {
        if (this.goodsListShow.length == this.goodsList.length) {
          this.isReachBottom = true;
          return;
        }
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
            }
            this.isLoading = false;
          }, 1500);
        }
      }
    },
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        this.getAllGoods();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
  .loading {
    text-align: center;
  }
  .icon-loading {
    display: inline-block;
    animation: rotate 2s linear infinite;
  }
  @keyframes rotate {
    to {
      transform: rotate(0);
    }
    from {
      transform: rotate(360deg);
    }
  }
}
</style>